const merge = require("webpack-merge");
const webpack = require("webpack");
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require("path");
const fs = require('fs');

const dir = require("./dir");

const pathToName = require(path.join(dir.frameworkCommonDir, 'driver')).pathToName;
const resolve = require("./driver").resolve;
const webpackPromise = require("./driver").webpackPromise;

const common = require("./webpack.common.js");

// 构建前清理
const CleanWebpackPlugin = require("clean-webpack-plugin");

module.exports = async (source_path) => {
  let source_name = pathToName(source_path);

  let config_file = path.join(resolve(source_path), 'webpack/dll.js');

  if (!fs.existsSync(config_file)) return;

  let entry = {};

  entry[source_name] = require(config_file);

  if (entry[source_name].length <= 0) return;

  let config = merge(common, {
    mode: "production",
    entry: entry,
    output: {
      filename: "index.js",
      path: path.join(dir.binDir, source_name, 'dll'),
      // library: "[name]_[chunkhash]"
      library: "[name]_[chunkhash]"
    },
    plugins: [
      new CleanWebpackPlugin(),
      new webpack.DllPlugin({
        path: path.join(dir.binDir, source_name, 'dll', "index.manifest.json"),
        name: "[name]_[chunkhash]"
      }),
    ]
  });

  await webpackPromise(config);


};
